//index.js
//获取应用实例
const app = getApp()
const $api = require('../../utils/api.js').API;
var login = require('../../utils/login.js');
Page({
  data: {
    imgUrl:app.globalData.imgUrl,
    city:'全部',
    banners:[],
    puid:0,
    scoreShow:1,
    info:{},
    advertShow:true,
    uid:0,
    company:[],
    pageShow:true,
    page:1, 
    gg:[],
    subscribe:false,
    lease:[]
  },
  //事件处理函数
  onShow: function(){
    this.setData({
      company:[],
      pageShow:true,
      page:1
    })
    let pages = getCurrentPages();
    // 数组中索引最大的页面--当前页面
    let currentPage = pages[pages.length-1];
    var e = currentPage.options   
    wx.setStorageSync('sharePuid',e.puid)
    wx.setStorageSync('shareUid',e.uid)
   
    var that = this
    setTimeout(function() {
      that.setData({
        advertShow:false
      })
    },3000)
    this.getBanner();
    this.getGg()
    wx.showLoading()
    this.getCompany();
    //this.getScoreSet();
    this.getLease()
    if(wx.getStorageSync('uid')){
      this.getWx()
    }
  },
  getLease:function(){
    var that = this;
    var data = {}
    $api.get('company/is_lease',data).then(res => {
      //请求成功
      if(res.data.code == 1){
        that.setData({
          lease:res.data.data.data
        })
      }
    })
  },
  getScoreSet:function(){
    var that = this;
    var data = {}
    $api.get('score/set',data).then(res => {
      //请求成功
      if(res.data.code == 1){
        that.setData({
          scoreShow:res.data.data
        })
      }
    })
  },
  getGg:function(){
    var that = this;
    var data = {'type':6}
    $api.get('banner',data).then(res => {
        //请求成功
        if(res.data.code == 1){
          that.setData({
            gg:res.data.data
          })
        }
    })
  },
  getBanner:function(){
    var that = this;
    var data = {}
    $api.get('banner',data).then(res => {
        //请求成功
        if(res.data.code == 1){
          that.setData({
            banners:res.data.data
          })
        }
    }).catch(err => {
        //请求失败
        //console.log('error','请求失败');
    })
  },
  getCompany:function(){
    var that = this;
    if(this.data.city == '全部'){
      var city = '';
    }else{
      var city = this.data.city;
    }
    var data = {
      page:this.data.page,
      city:city
    }
    $api.get('company/hot',data).then(res =>{
        if(res.data.code == 1){
          if(res.data.data.last_page <= that.data.page){
              that.setData({
                  pageShow:false
              })
          }
          wx.hideLoading()
          if(that.data.page == 1){
            that.setData({
              company:res.data.data.data
            })
          }else{
              var company = that.data.company
              that.setData({
                company:company.concat(res.data.data.data)
              })
          }
      }
    })
  },
  jump:function(e){
    var url = e.currentTarget.dataset.key
    var that = this
    switch(url){
      case '/pages/factory/index': 
        that.login(url,3)
        break;
      case '/pages/oa/index':
        wx.showToast({
          title:'请下载APP免费体验试用!!',
          duration:3000,
          mask:true, 
          icon:'none',
        })
        return;
        that.OARole();
        break;
      case '/pages/job/index':
      case '/pages/job/index?star=1':
      case '/pages/pointsMall/index':
      case '/pages/company/index':
        that.login(url,null)
        break;
      default:
        wx.navigateTo({
          url:url
        })
        break;
    }
  },
  OARole:function(){
    if(!wx.getStorageSync('uid')){
      wx.navigateTo({
        url:'/pages/index/guide'
      })
      return;
    }else{
      if(wx.getStorageSync('role') == 1){
        wx.navigateTo({
          url:'/pages/login/index'
        })
        return;
      }
      if(wx.getStorageSync('role') == 4){
        wx.showToast({
          title:'暂无权限!!',
          duration:3000,
          mask:true, 
          icon:'none',
        })
        return;
      }
      this.jumpOA();
    }
  },
  jumpOA:function(){
    var data = {
      'uid':wx.getStorageSync('uid'),
      'role':wx.getStorageSync('role')
    }
    var that = this
    $api.get('oa/type',data).then(res =>{
      if(res.data.code == 1){
        //已经开通
        wx.navigateTo({
          url:'/pages/oa/index'
        })
      }else{
        //暂未开通
        wx.navigateTo({
          url:'/pages/oa/login/index'
        })
      }
    })
  },
  toAdDetial:function(e){
    var id = e.currentTarget.dataset.id
    var url = '/pages/company/info?id='+id
    this.login(url,2)
  },
  onReachBottom: function(){
    if(this.data.pageShow){
        //当前不是最后一页可加载
        this.setData({
            page: this.data.page + 1,
        })
        wx.showLoading()
        this.getCompany();
    }
},
  onShareAppMessage: function(){
    return {
        title: "天意美业直通车",
        path: "/pages/index/index?puid="+wx.getStorageSync('uid')
    };
  },
  login:function(url,role){
    if(!wx.getStorageSync('uid')){
      //没有缓存 跳转授权页面
      wx.navigateTo({
        url:'/pages/index/guide'
      })
    }else{
      //验证手机号码是否存在
      this.checkphone(url,role);
    }
  },
  //查询手机号  是否注册
  checkphone:function(url,role){
    var data = {
      'uid':wx.getStorageSync('uid')
    }
    $api.post('member/phone',data).then(res =>{
      if(res.data.code == 1){
        this.checkRole(url,role)
      }else{
        wx.navigateTo({
          url:'/pages/index/phone'
        })
      }
    })
  },
  getWx:function(){
    var data = {'uid':wx.getStorageSync('uid')}
    var that = this
    $api.get('user/wxUser',data).then(res => {
        //请求成功
        if(res.data.code == 1){
            
        }else{
          that.setData({
            subscribe:true
          })
        }
    })
  },
  checkRole:function(url,role){
    if(wx.getStorageSync('role') == 1){
      wx.navigateTo({
        url:'/pages/login/index'
      })
      return
    }
    //wx.setStorageSync('role',2)
    //console.log(role,wx.getStorageSync('role'))
    if(!role || wx.getStorageSync('role') == role){
      wx.navigateTo({
        url:url
      })
    }else{
      wx.showToast({
        title:'暂无权限!!',
        duration:3000,
        mask:true, 
        icon:'none',
      })
      return;
    }
  },
  buy1:function(){
    //查看当前机构是否购买过  有群发消息的权限
    var that = this
    var data = {
        uid:wx.getStorageSync('uid')
    }
    $api.get('vip/message',data).then(res => {
        if(res.data.code == 1){
            wx.navigateTo({
                url:'/pages/job/index?star=1'
            })
        }else{
          wx.showToast({
            title: '白金VIP会员有此权限!!',//提示文字
            duration:3000,//显示时长
            mask:true,//是否显示透明蒙层，防止触摸穿透，默认：false  
            icon:'none', //图标，支持"success"、"loading"   
            success:function(){
              setTimeout(function(){
                wx.navigateTo({
                  url: '/pages/vip/index'
                })
              },3000) //延迟时间 这里是
            }
          })
        }
    })
}
})
